package com.think.teaching.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.stereotype.Service;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.ExpStatMapper;
import com.think.teaching.model.ExpStat;
import com.think.teaching.model.param.ExportExpStatParam;

@Service
public class ExpStatServiceImpl extends BaseServiceImpl<ExpStatMapper, ExpStat> {
	
	/**
	 * 获取进行实验的次数
	 * @param expId
	 * @return
	 */
	public Integer GetExpCount(Integer expId) {
		QueryWrapper<ExpStat> queryWrapper = new QueryWrapper<ExpStat>();
		queryWrapper.eq("exp_id", expId);
		return count(queryWrapper);
	}
	/**
	 * 获取完成实验的人数
	 * @param expId
	 * @return
	 */
	public Integer GetExpFinishCount(Integer expId) {
		QueryWrapper<ExpStat> queryWrapper = new QueryWrapper<ExpStat>();
		queryWrapper.eq("exp_id", expId).eq("status", 3);
		return count(queryWrapper);
	}
	
	public List<ExpStat> GetExpFinishedReport() {
		QueryWrapper<ExpStat> queryWrapper = new QueryWrapper<ExpStat>();
		queryWrapper.eq("status", 3);
		return list(queryWrapper);
	}
	/**
	 * 获取完成实验的人数
	 * @param expId
	 * @return
	 */
	public Integer GetExpStudentCount(Integer expId) {
		return baseMapper.GetExpStudent(expId).size();
	}
	
	public IPage<Map<String, Object>> ListStat(Integer id,Integer page,Integer count){
		IPage<Map<String, Object>> iPage = new Page<Map<String, Object>>(page, count);
		return baseMapper.ListStat(iPage, id,null,null);
	}
	public IPage<Map<String, Object>> ListStatByCourse(Integer id,Integer page,Integer count){
		IPage<Map<String, Object>> iPage = new Page<Map<String, Object>>(page, count);
		return baseMapper.ListStatByCourse(iPage, id);
	}
	
	public IPage<Map<String, Object>> ListUserStat(Integer id,Integer page,Integer count,Integer userId){
		IPage<Map<String, Object>> iPage = new Page<Map<String, Object>>(page, count);
		return baseMapper.ListUserStat(iPage, id,userId);
	}
	
	public Map<String, Object> GetStatTime(Integer expId) {
		Map<String, Object> map = new HashedMap<String, Object>();
		map.put("total", baseMapper.GetTotalTime(expId));
		map.put("0-10", baseMapper.GetStatTime(0, 10*60, expId));
		map.put("10-30", baseMapper.GetStatTime(10*60, 30*60, expId));
		map.put("30-50", baseMapper.GetStatTime(30*60, 50*60, expId));
		map.put("50-60", baseMapper.GetStatTime(50*60, 70*60, expId));
		map.put("70-90", baseMapper.GetStatTime(70*60, 90*60, expId));
		map.put("90-1000", baseMapper.GetStatTime(90*60, 1000*60, expId));
		return map;
	}
	
	public Map<String, Object> GetStatScore(Integer expId) {
		Map<String, Object> map = new HashedMap<String, Object>();
		map.put("0-60", 3);
		map.put("60-70", 3);
		map.put("70-80", 6);
		map.put("80-90", 5);
		map.put("90-100", 6);
		return map;
	}
	
	public ExpStat GetLastRecord(Integer expId,Integer userId) {

		
		return baseMapper.GetLastRecord(userId, expId);
	}
	
	
	public List<Map<String, Object>> ExportStat(ExportExpStatParam param) {
	  return baseMapper.getAllListStat(param.getExpId(), param.getStartTime(), param.getEndTime());
//		return true;
	}
}
